// DiabloClone.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "DiabloClone.h"
#include "Graphics\DxRenderer.h"
#include "Graphics\DxShaderManager.h"
#include "Utility\Vector4.h"
#include "Utility\FileManager.h"
#include "Input\DxInputManager.h"
#include "GameplayState.h"
#include "GameState.h"

// Forward declarations of functions included in this code module:
extern HWND	RegisterWindow(HINSTANCE hInstance);
extern void CloseRenderWindow(HWND hWnd);
extern void HandleMessages(HWND hWnd);

bool			g_bExitGame = false;
DxInputManager* g_pInputManager = 0;
GameState*		g_pGameState = 0;


int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
	HWND hWnd;	

	hWnd = RegisterWindow(hInstance);

	DxRenderer* renderer = DxRenderer::GetInstance();
	renderer->CreateDevice(hWnd);
	renderer->SetPerspective(45.0f, 0.1, 1000.0f);

	TCHAR szDirectory[MAX_PATH] = "";
	GetCurrentDirectory(sizeof(szDirectory) - 1, szDirectory);
	FileManager::SetRootPath((char*)&szDirectory);	
	
	g_pInputManager = new DxInputManager(hWnd);

	//setup timing
	LARGE_INTEGER liEnd, liStart;	
	QueryPerformanceFrequency(& liStart);

	double dCyclesPerMilliSecond = 1.0 / (double)liStart.QuadPart;

	
	float fDelta = 0.0f;
	g_pGameState = new GameplayState();
	while(g_pInputManager->Update(fDelta))
	{
		if( g_bExitGame )
			break;

		HandleMessages(hWnd);

		g_pGameState->Update(fDelta);

		//begin drawing
		renderer->BeginDraw();

		g_pGameState->Draw();

		//end drawing and show scene
		renderer->EndDraw();

		Sleep(1);

		QueryPerformanceCounter(&liEnd);
		double dCylces = double(liEnd.QuadPart - liStart.QuadPart);
		fDelta = (float)(dCylces * dCyclesPerMilliSecond);
		liStart = liEnd;		
	}

	//perform clean up

	CloseRenderWindow(hWnd);

	delete DxShaderManager::GetInstance();
	delete renderer;
	delete g_pInputManager;	
	delete g_pGameState;
	

	return 0;
}

void InputEvent(eInputEventType type, u32 button)
{
	g_pGameState->InputEvent(type, button);
}








